<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/9/16
 * Time: 11:38
 */

namespace app\service;
use think\Db;

/**
 * 收支统计服务层
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class IncomeService extends BaseService
{
    /**
     * 列表条件
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月24日13:22:12
     * @param    [array]          $params [输入参数]
     */
    public static function ListWhere($params = [])
    {

        $where = [];

        //时间
        if(!empty($params['create_time']))
        {

            $where[] = ['create_time', '>', explode(' - ',$params['create_time'])[0]];
            $where[] = ['create_time', '<', explode(' - ',$params['create_time'])[1]];
        }

        if(!empty($params['pay'])&&intval($params['pay'])>0)
        {

            $where[] = ['pay', '=', $params['pay']];

        }

        if(!empty($params['type'])&&intval($params['type'])>0)
        {

            $where[] = ['type', '=', $params['type']];

        }

        $where[] = ['id', '>', 0];


        return $where;
    }


    public static function IncomeDataList($params){

    }
    /**
     * 数据处理
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param   [array]          $data [广告数据 二维数组]
     */
    public static function DealWith($data){

        if(!empty($data) && is_array($data))
        {
            foreach($data as &$v)
            {
                $v['pay_title']=$v['pay']==1?'微信':'支付宝';
                $v['type_title']=$v['type']==1?'支出':'收入';
                $v['total_price']=PriceNumberFormat($v['total_price'],2);
                $v['create_time']=date('Y-m-d',strtotime($v['create_time']));
            }
        }

        return $data;
    }

    public static function IncomeTotal($where){
        //当前总收入，微信，支付宝,总支出
        $total_income=0.00;
        $total_out=0.00;
        $total_wx=0.00;
        $total_ali=0.00;
        $total=Db::name('log_income')->where($where)->select();
        foreach ($total as $v){
            if($v['type']==1){
                $total_out+=$v['total_price'];
            }else{
                $total_income+=$v['total_price'];
                if($v['pay']==1){
                    $total_wx+=$v['total_price'];
                }else{
                    $total_ali+=$v['total_price'];
                }
            }
        }
        $data=[
            'total_income'=>PriceNumberFormat($total_income,2),
            'total_out'=>PriceNumberFormat($total_out,2),
            'total_wx'=>PriceNumberFormat($total_wx,2),
            'total_ali'=>PriceNumberFormat($total_ali,2)
        ];
        return DataReturn('获取成功',0,$data);

    }
}